﻿using AutoMapper;
using BackData;
using Domain;
using Infratructure;
using MediatR;
using RBAC.API.Command.Permission;
using Yitter.IdGenerator;

namespace RBAC.API.Handler.Permission
{
    public class PermissionUpdateHandler:IRequestHandler<PermissionUpdateCommand,ApiResult<int>>
    {

        private readonly IRepository<PermissionModel> permissionway;
        private readonly IMapper mapper;
        private readonly ILogger<PermissionGetHandler> log;

        public PermissionUpdateHandler(IRepository<PermissionModel> permissionway, IMapper mapper, ILogger<PermissionGetHandler> log)
        {
            this.permissionway = permissionway;
            this.mapper = mapper;
            this.log = log;
        }

        /// <summary>
        /// 权限修改
        /// </summary>
        /// <param name="request">参数</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>影响行数</returns>
        public async Task<ApiResult<int>> Handle(PermissionUpdateCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();
            var value = mapper.Map<PermissionModel>(request);
          
            var result = await permissionway.Updateway(value);
            if (result > 0)
            {
                res.Message = "权限修改成功";
                res.Code = StateCode.OK;
                res.Data = result;
            }
            else
            {
                res.Message = "权限修改失败";
                res.Code = StateCode.Fail;
            }
            return res;
        }
    }
}
